cd /Users/Laura/Documents/Research/PDHosp/Replication
log using WMKM_REStat_state_regs.log, replace
/************************************************************
PROGRAM: WMKM_REStat_state_regs.do
PURPOSE: State-level RD analysis of hospital and ED data for 
		 Wherry, Miller, Kaestner, and Meyer
DATE: 	 February 7, 2017
NOTES:	 To run, (1) change directory above to a local directory. 
		 (2) Create subfolders called src, data, and output. 
		 (3) Put source hospital and ED data files in src directory. 
CONTACT: Laura Wherry, lwherry@mednet.ucla.edu	 
*************************************************************/
set more off

global xvars post post#c.x_c post#c.x_c2 x_c x_c2 ///
i.fips i.bmonth
global xvars2 post post#c.x_c post#c.x_c2 x_c x_c2 ///
i.fips i.fips#c.x_c i.fips#c.x_c2 i.fips#post#c.x_c i.fips#post#c.x_c2 ///
i.bmonth

import excel using src/discontinuities.xlsx, first
save data/discont, replace

capture program drop rdstate
program define rdstate

insheet using "src/`1'.csv", c clear

*define sample
keep if inrange(byear,1979,1987)
drop if inrange(bmonth,1,9) & byear==1979
drop if inrange(bmonth,10,12) & byear==1987

*create variable for regression
sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
capture drop post
gen post=1*(x_c>=0)
gen x_c2=x_c^2

*create dependent variables
replace preg=0 if preg==.
gen np=count-preg
gen lognp=log(count-preg)
gen nchronic=np-hcupchronic
gen lognchronic=log(count-preg-hcupchronic)
gen logchronic=log(hcupchronic)

*if any zero cells, drop
if `2'==2{
	bysort fips: gen obs=_N
	gen dropme=1*(obs<96)
	bysort fips: egen lownp=min(np)
	replace dropme=1 if lownp==0
	save data/temp, replace
	egen flag=tag(fips)
	keep if dropme==0 & flag
	keep dropme fips
	save data/sample, replace
	use data/temp, clear
	keep if dropme==0
}
if `2'==3{
	merge m:1 fips using data/sample, keep(match)
}

*assign discontinuity by state and race group
capture drop discont
merge m:1 fips using data/discont, keep(match) nogen
gen discont=0
replace discont=difyrs`3'_`2'
gen postdiscont=post*discont
gen invpop=1/population
save data/`1'`2', replace

*discontinuity description
*su discont, d

*regression analysis
!rm output/st`1'`2'*
foreach x in lognp logchronic lognchronic {
	di "Outcome `x', data`1', eligibility est sample `3'"
	
	***weighted
	*restricted
	qui: regress `x' postdiscont $xvars [aweight=population], vce(cluster fips)
	outreg2 using output/st`1'`2', append excel bdec(3) ctitle("`x'", "Restricted", "Pop weighted") 
	*flexible
	qui: regress `x' postdiscont $xvars2 [aweight=population], vce(cluster fips)
	outreg2 using output/st`1'`2', append excel bdec(3) ctitle("`x'", "Flexible", "Pop weighted")
	
	***unweighted
	*restricted
	qui: regress `x' postdiscont $xvars, vce(cluster fips)
	outreg2 using output/st`1'`2', append excel bdec(3) ctitle("`x'", "Restricted", "Unweighted") 
	*flexible
	qui: regress `x' postdiscont $xvars2, vce(cluster fips)
	outreg2 using output/st`1'`2', append excel bdec(3) ctitle("`x'", "Flexible", "Unweighted")	

}

end

rdstate bystate2009 1 state
rdstate blackbystate 2 state
rdstate nonblackbystate 3 state

rdstate erbystate2009 1 state
rdstate blackerbystate 2 state
rdstate notblackerbystate2009 3 state

rdstate bystate1999 1 state
rdstate blackbystate1999 2 state
rdstate nonblackbystate1999 3 state 


log close
